import re, time, os
from xx_spider.config.wash_config import *
from lxml import etree


class FileUtil:
    def __init__(self):
        pass

    @staticmethod
    def write_txt(path, content):
        with open(path, "w", encoding="u8") as f:
            f.write(content)

    @staticmethod
    def get_path(file_dir, postfix):
        file_name = int(time.time())
        while True:
            path = file_dir + "/" + str(file_name) + "." + postfix
            if not os.path.exists(path):
                return path
            file_name += 1

    @staticmethod
    def fuse_path(path_list):
        p_list = []
        for path in path_list:
            if len(path.strip()) > 0:
                path = path.replace("\\", "/").strip()
                if path[0] == "/":
                    path = path[1:]
                if path[-1] == "/":
                    path = path[:-1]
                p_list.append(path)
        return "/".join(p_list)


class Washer:
    @staticmethod
    def purify_data(data, get_number=False):
        patten = re.compile(r'[^|,，【】 、?？《》<>‘’*&%￥#@!！$^（）+-.…。();；~_“”A-Za-z0-9\u4E00-\u9FA5]')
        result, number = patten.subn("*", data)
        if get_number:
            return result, number
        return result

    @staticmethod
    def clean_messy_code(content, get_cnt=False):
        cnt = 0
        if content is not None:
            for key, value in replace_dict.items():
                pattern = re.compile(key)
                content, number = pattern.subn(value, content)
                cnt += number
        if get_cnt:
            return content, cnt
        return content

    @staticmethod
    def clean_js_tag(content, *tags):
        if content is None:
            return None
        for tag in tags:
            pattern = "<" + tag + "[\s\S]*?</" + tag + ">"
            content = re.sub(pattern, "", content)
        return content


class ParseUtil:
    def parse_by_xpath(self, data, path, mode="fuse"):
        if data is None:
            return None
        if type(data) == str:
            data = etree.HTML(data)
        assert type(data) == etree._Element, "input data type is {0}!".format(type(data))
        assert mode in ["get", "list", "fuse"], "check the mode, current mode is {0}".format(mode)
        ret_datas = data.xpath(path)
        if len(ret_datas) > 0:
            if mode == "list":
                return [etree.tostring(r_data, encoding="utf-8").decode("utf-8").strip() for r_data in ret_datas]
            if mode == "get":
                return etree.tostring(ret_datas[0], encoding="utf-8").decode("utf-8").strip()
            if mode == "fuse":
                return "\n".join(
                    [etree.tostring(r_data, encoding="utf-8").decode("utf-8").strip() for r_data in ret_datas])
            raise Exception("mode error")
        return [] if mode == "list" else None

    def parse_by_regex(self, data, pattern, mode="fuse"):
        if data is None:
            return None
        if type(data) == etree._Element:
            data = etree.tostring(data, encoding="utf-8").decode("utf-8")
        assert type(data) == str, "input data type is {0}".format(type(data))
        assert mode in ["get", "list", "fuse"], "check the mode, current mode is {0}".format(mode)
        ret_datas = re.findall(pattern, data)
        if len(ret_datas) > 0:
            if mode == "list":
                return [r_data.strip() for r_data in ret_datas]
            r_data = ret_datas[0] if mode == "get" else "\n".join(ret_datas)
            return r_data.strip()
        return [] if mode == "list" else None


if __name__ == '__main__':
    l = ['ww.bbc/', "/asdf\\t/1.html"]
    print(FileUtil.fuse_path(l))
